package mest.util;

import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @auther Machenike
 * @create 2019-06-25 23:02
 */


public class FreeMarket {

    //两处地方需修改
    private static String driver="com.mysql.jdbc.Driver";
    private static String url="jdbc:mysql://127.0.0.1:3306/standard?useSSL=true&useUnicode=true&characterEncoding=utf-8";
    private static String user="root";
    private static String password="root";

    private static Configuration config;

    private static Connection conn;
    private static DatabaseMetaData dmd;


   public FreeMarket() throws IOException   {

            //实例化
            config=new Configuration(Configuration.getVersion());
            //设置模板所在路径
            String path=this.getClass().getClassLoader().getResource("").getPath();
            System.out.println(path);
            config.setDirectoryForTemplateLoading(new File(path));

    }

    //1.初始化配置


    //2.获取模板生成相应数据
    public static void process(Map<String, Object> param, String tempName,String fileDir) {
        try {
            Template template = config.getTemplate(tempName);
            FileOutputStream stream=new FileOutputStream(fileDir);
            template.process(param,new OutputStreamWriter(stream));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static{
        try {
            Class.forName(driver);
            conn=DriverManager.getConnection(url,user,password);
            dmd=conn.getMetaData();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //获取所有表
    public static List<Table> getAllTables(){
        ResultSet rs=null;
        List<Table> tableList=new ArrayList<>();
        try {
            //1.遍历获取表
            rs=dmd.getTables(null,null,null,new String[]{"TABLE"});
            while(rs.next()){
                Table table=new Table();
                tableList.add(table);
                String tableName=rs.getString("TABLE_NAME");
                //2.遍历对应表上所有字段
                List<Field> fieldList= getColumns(tableName);

                //3.包装
                table.setClassName(tableName);
                table.setFiledList(fieldList);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return tableList;
    }

    //包装字段名
    public static List<Field> getColumns(String tableName){
        List<Field> fieldList=new ArrayList<>();
        ResultSet rs=null;
        try {
            rs=dmd.getColumns(null,"%",tableName,"%");
            while(rs.next()){
//                System.out.println("列:"+rs.getString("COLUMN_NAME")+"类型:"+rs.getString("TYPE_NAME")+
//                                "注释:"+rs.getString("REMARKS")
                //包装
                Field field=new Field();
                fieldList.add(field);
                field.setFiledName(rs.getString("COLUMN_NAME"));
                field.setUpperfiledName(rs.getString("COLUMN_NAME").substring(0,1).toUpperCase()+
                        rs.getString("COLUMN_NAME").substring(1)
                );
                field.setRemarks(rs.getString("REMARKS"));
                field.setFiledType(switchType(rs.getString("TYPE_NAME")));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return fieldList;
    }

    public static String switchType(String cloumnType){
        String fieldType=null;

        if(cloumnType.equals("VARCHAR")){
            fieldType="String";
        }else if(cloumnType.equals("BIGINT")){
            fieldType="Long";
        }else if(cloumnType.equals("INT")){
            fieldType="Integer";
        }else if(cloumnType.equals("DATETIME")){
            fieldType="Date";
        }else{
            fieldType="String";
        }
        return fieldType;
    }
    public static void main(String[] args) throws IOException {

        new FreeMarket();
        List<Table> tableList=getAllTables();
        for(Table table:tableList){
            Map<String,Object> param=new HashMap<String, Object>();
            param.put("table",table);
            FreeMarket.process(param,"pojo.ftl","C:\\Users\\Machenike\\Desktop\\entity\\"+table.getClassName()+".java");
        }
    }//main
}


//Field
//public class Field {
//
//    private String filedType;
//
//    private String filedName;
//
//    private String upperfiledName;
//
//    private String remarks;
//
//    public String getRemarks() {
//        return remarks;
//    }
//
//    public void setRemarks(String remarks) {
//        this.remarks = remarks;
//    }
//
//    public String getFiledType() {
//        return filedType;
//    }
//
//    public void setFiledType(String filedType) {
//        this.filedType = filedType;
//    }
//
//    public String getFiledName() {
//        return filedName;
//    }
//
//    public void setFiledName(String filedName) {
//        this.filedName = filedName;
//    }
//
//    public String getUpperfiledName() {
//        return upperfiledName;
//    }
//
//    public void setUpperfiledName(String upperfiledName) {
//        this.upperfiledName = upperfiledName;
//    }
//}



//Table
//public class Table {
//    private String className;
//
//    private List<Field> FiledList=new ArrayList<>();
//
//    public String getClassName() {
//        return className;
//    }
//
//    public void setClassName(String className) {
//        this.className = className;
//    }
//
//    public List<Field> getFiledList() {
//        return FiledList;
//    }
//
//    public void setFiledList(List<Field> filedList) {
//        FiledList = filedList;
//    }
//}



//pojo.ftl
//package cn.itrip.beans.pojo;
//
//        import java.io.Serializable;
//        import java.util.Date;
//
//public class ${table.className} implements Serializable {
//
//<#list table.filedList as field>
//<#if field.remarks!=''>
////${field.remarks}
//</#if>
//private ${field.filedType} ${field.filedName};
//</#list>
//
//<#list table.filedList as field>
//public void set${field.upperfiledName} (${field.filedType}  id){
//        this.${field.filedName}=${field.filedName};
//        }
//
//public  ${field.filedType} get${field.upperfiledName}(){
//        return this.${field.filedName};
//        }
//</#list>
//        }
